class VisitorsController < ApplicationController

  before_filter :require_admin, :only => [:choose_event]

  def transition
    @event = current_event
    render :layout => false
  end

  def choose_event
    #
    Event.stop_all
    #@events = Event.find(:all, :conditions => ['starts_at >= ?', Date.today], :order => "starts_at ASC")
    #  @events = Event.all
    #render :layout => false
    flash[:notice] = "Event has been stopped."
    redirect_to(dashboard_path(current_user)) and return
  end

  def signin
    current_user_session.destroy if current_user_session
    @visitor = Visitor.new
    @event = Event.find(params[:event_id])
    @event.start_event
  end

  def check_signin
    @name = params[:visitor][:name]
    @visitor = Visitor.all(:conditions => ["name LIKE ?", "%#{params[:visitor][:name]}%"], :order => "updated_at DESC")
    if @visitor.empty?
      @visitor = Visitor.new(params[:visitor])
      render :action => "new", :name => params[:visitor][:name], :layout => false
    else
      render :action => "show_like_visitors", :layout => false
    end
  end

  def attend_event
    @visitor = Visitor.find(params[:visitor_id])
    @event = current_event
    @visitor.attend(@event)
    if @visitor.save
      flash[:notice] = 'Thank you, you have been successfully signed in.'
      redirect_to visitors_transition_path
    else
      raise "blah"
    end
  end

  def show_like_visitors
    #doesnt normally get called?
    @visitors = Visitor.all(:conditions => ["name LIKE ?", "%#{params[:visitor][:name]}%"], :order => "updated_at DESC")
  end
 
  # GET /visitors
  # GET /visitors.xml
  def index
    @visitors = Visitor.all

  end

  # GET /visitors/1
  # GET /visitors/1.xml
  def show
    @visitor = Visitor.find(params[:id])

    render :layout => false
  end

  # GET /visitors/new
  # GET /visitors/new.xml
  def new
    @visitor = Visitor.new(params[:visitor])
    if (!params[:name].empty? && @visitor.name.nil?)
      @visitor.name = params[:name]
    end
    #render :layout => false
  end

  # GET /visitors/1/edit
  def edit
    @visitor = Visitor.find(params[:id])
  end

  # POST /visitors
  # POST /visitors.xml
  def create
    @visitor = Visitor.new(params[:visitor])
    @visitor.attend(current_event)
    if @visitor.save
      flash[:notice] = 'Thank you, you have been successfully signed in.'
      redirect_to visitors_transition_path
    else
      flash[:notice] = 'Sorry, but there were problems sigining in. Please try again.'
      redirect_to(visitors_signin_path(current_event))
    end
    
  end

  # PUT /visitors/1
  # PUT /visitors/1.xml
  def update
    @visitor = Visitor.find(params[:id])

    respond_to do |format|
      if @visitor.update_attributes(params[:visitor])
        flash[:notice] = 'Visitor was successfully updated.'
        format.html { redirect_to(@visitor) }
        format.xml  { head :ok }
      else
        format.html { render :action => "edit" }
        format.xml  { render :xml => @visitor.errors, :status => :unprocessable_entity }
      end
    end
  end

  # DELETE /visitors/1
  # DELETE /visitors/1.xml
  def destroy
    @visitor = Visitor.find(params[:id])
    @visitor.destroy

    respond_to do |format|
      format.html { redirect_to(visitors_url) }
      format.xml  { head :ok }
    end
  end

  helper_method :current_event
  helper_method :active_volunteers

  def set_event(event)
    $CURRENT_EVENT = event
  end

  private

  def current_event
    return @current_event if defined?(@current_event)
    @current_event = Event.find_by_is_current(true)
    if @current_event.nil?
      #raise "no current event set"
    else
      return @current_event
    end
  end

  def active_volunteers
    @active_volunteers = Person.find(:all, :conditions => {:is_active => true, :is_volunteer => true}, :order => "updated_at DESC")
  end

end
